<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-title">找回密码</div>
      <div class="login-subtitle">请输入注册时的邮箱和手机号进行验证</div>
      
      <el-form :model="form" :rules="rules" ref="formRef" label-width="0">
        <el-form-item prop="email">
          <el-input
            v-model="form.email"
            placeholder="请输入注册时的邮箱"
            size="large"
            :prefix-icon="Message"
            clearable
            :disabled="loading"
          />
        </el-form-item>
        <el-form-item prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入注册时的手机号"
            size="large"
            :prefix-icon="Phone"
            clearable
            :disabled="loading"
          />
        </el-form-item>
        <el-form-item>
          <el-button 
            class="login-button" 
            type="primary" 
            :loading="loading"
            @click="onSubmit"
          >
            {{ loading ? '验证中...' : '验证并重置密码' }}
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="login-footer">
        已想起密码？<el-link type="primary" @click="goToLogin" :disabled="loading">返回登录</el-link>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Message, Phone } from '@element-plus/icons-vue'
import { userStorage } from '@/utils/storage'
import {verifyUserIdentity} from '@/api/auth'
const router = useRouter()
const formRef = ref()
const loading = ref(false)
const form = ref({
  email: '',
  phone: '',
})

// 验证规则
const rules = {
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { type: 'email', message: '邮箱格式不正确', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { pattern: /^1[3-9]\d{9}$/, message: '手机号格式不正确', trigger: 'blur' },
  ],
}

// 提交验证
async function onSubmit() {
  if (!formRef.value) return
  
  try {
    // 前端表单验证
    await formRef.value.validate()
    
  let res=  await verifyUserIdentity(form.value)
    console.log(res);
    
    if(res.code===200){
      ElMessage.success(`${res.message}`)
          router.push('/reset-password')
    }
     if(res.code===403){
      // ElMessage.error(`${res.message}`)
    }
  

    // 设置加载状态
    loading.value = true
    
    localStorage.setItem("storageUser",JSON.stringify(form.value))

//  verifyUserIdentity
    // userStorage.setPasswordResetVerification(verificationData)
    // 显示提示信息

    
    // 跳转到重置密码页面进行实际验证和密码重置

    
  } catch (error) {
    console.error('Form validation error:', error);
    ElMessage.error('请检查输入信息')
  } finally {
    // 重置加载状态
    loading.value = false
  }
}

function goToLogin() {
  if (!loading.value) {
    router.push('/login')
  }
}
</script>

<style scoped>
.login-container {
  height: 100vh;
  width: 100vw;
  display: flex;
  align-items: center;
  justify-content: center;
  background-image: url('@/assets/images/login-bg.svg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: 20px;
  position: relative;
  overflow: hidden;
  box-sizing: border-box;
}
.login-container::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.1);
  backdrop-filter: blur(2px);
  z-index: 1;
  animation: backgroundPulse 8s ease-in-out infinite;
}
@keyframes backgroundPulse {
  0%, 100% { opacity: 0.1; }
  50% { opacity: 0.15; }
}
.login-card {
  position: relative;
  z-index: 2;
  background: rgba(255, 255, 255, 0.6);
  backdrop-filter: blur(16px) saturate(180%);
  -webkit-backdrop-filter: blur(16px) saturate(180%);
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 16px;
  box-shadow: 0 20px 40px rgba(0, 0, 0, 0.1);
  padding: 40px;
  width: 100%;
  max-width: 400px;
  max-height: 90vh;
  overflow-y: auto;
}
.login-title {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  margin: 0 0 8px 0;
  text-align: center;
  animation: titleGlow 4s ease-in-out infinite;
}
@keyframes titleGlow {
  0%, 100% { text-shadow: 0 0 8px rgba(102, 126, 234, 0.4); }
  50% { text-shadow: 0 0 25px rgba(102, 126, 234, 0.8); }
}
.login-subtitle {
  color: #7f8c8d;
  font-size: 14px;
  margin: 0 0 30px 0;
  text-align: center;
}
.el-form {
  margin-bottom: 20px;
}
.el-form-item {
  margin-bottom: 20px !important;
}
.el-form :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(5px);
  border: 1px solid rgba(255, 255, 255, 0.3);
  transition: all 0.3s ease;
}
.el-form :deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  border-color: rgba(102, 126, 234, 0.3);
}
.el-form :deep(.el-input__wrapper.is-focus) {
  box-shadow: 0 0 0 2px rgba(102, 126, 234, 0.2);
  border-color: #667eea;
}
.login-button {
  width: 100%;
  height: 48px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: all 0.3s ease;
  position: relative;
  overflow: hidden;
}
.login-button::before {
  content: '';
  position: absolute;
  top: 0;
  left: -100%;
  width: 100%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255, 255, 255, 0.2), transparent);
  transition: left 0.5s;
}
.login-button:hover::before {
  left: 100%;
}
.login-button:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 16px rgba(102, 126, 234, 0.3);
}
.login-button:active {
  transform: translateY(0);
}
.login-footer {
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
  margin-top: 18px;
}
.login-footer .el-link {
  font-size: 14px;
}
@media (max-width: 480px) {
  .login-card {
    padding: 30px 20px;
    margin: 10px;
  }
  .login-title {
    font-size: 24px;
  }
}
</style>